import router from "@/router"
import { LOGIN, REGISTER, FIND_PASSWORD, HOME } from '@/router/routeMap'
import useUserStore from '@/store/user'

router.beforeEach((to, from) => {
  const user = useUserStore()
  const isNeedLogin = [].includes(to.name)
  const isNoNeedLogin = [LOGIN, REGISTER, FIND_PASSWORD].includes(to.name)

  if (user.isLogined) {
    if (isNoNeedLogin) return from.name || HOME

    return true
  } else {
    if (isNeedLogin) return { ...LOGIN, params: { redirect: from.name } }

    return true
  }
})
